package com.thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created with IntelliJ IDEA.
 * User: 5
 * Date: 16-12-20
 * Time: 上午11:13
 * To change this template use File | Settings | File Templates.
 */
public class ThreadMine extends Thread {
    //类内部不存在线程安全问题
//    static int i = 0;
    static  AtomicInteger integer = new AtomicInteger(0);

    @Override
    public void run() {
        while (integer.get() < 10) {
//            synchronized (ThreadMine.class) {
            integer.getAndIncrement();
            System.out.println(Thread.currentThread() + " -- " + integer);
//            }
        }
    }

    public static void main(String args[]) {
        new ThreadMine().start();    //两个独立的线程 不存在竞争问题   i不加static
        new ThreadMine().start();     //i加static后，类共享因此存在竞争问题。
    }
}
